package ca.usherbrooke.domus.sipnmove2.model.phone.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import ca.usherbrooke.domus.sipnmove2.model.call.Call;
import ca.usherbrooke.domus.sipnmove2.model.phone.NewCallListener;
import ca.usherbrooke.domus.sipnmove2.model.phone.Phone;

/**
 * Implementation of a phone
 */
public class PhoneImpl implements Phone {

	/**
	 * Listeners to notify of a new call
	 */
	private Set<NewCallListener> newCallListeners;
	
	/**
	 * Calls map
	 */
	private Map<String, Call> calls;
	
	/**
	 * Default constructor
	 */
	public PhoneImpl()
	{
		newCallListeners = new HashSet<NewCallListener>();
		calls = new HashMap<String, Call>();
	}

	@Override
	public void addNewCallListener(NewCallListener newCallListener) {
		newCallListeners.add(newCallListener);
	}

	@Override
	public void addCall(String callId, Call call) {
		this.calls.put(callId, call);
		notifyNewCallListener(call);
	}

	@Override
	public void quit() {
		System.out.println("Application quitted : bye !");
		System.exit(0);
	}

	/**
	 * Notify all listeners of a new call
	 * @param call
	 */
	private void notifyNewCallListener(Call call) {
		for(NewCallListener newCallListener : newCallListeners)
		{
			newCallListener.processNewCall(call);
		}
	}


	@Override
	public Call getCallById(String callId) {
		return calls.get(callId);
	}


	@Override
	public void removeCallById(String callId) {
		calls.remove(callId);
	}

}
